<template>
  <div>
    <my-form-drawer
      :title="title"
      :width="450"
      :visible="visible"
      @ok="edit"
      @cancel="onClose"
    >
      <a-form-model ref="form" :model="form" :rules="rules">
        <a-form-model-item label="集团名称" prop="name">
          <a-input v-model="form.name" />
        </a-form-model-item>
        <a-form-model-item label="管理员账号" prop="phone">
          <a-input v-model="form.phone" placeholder="请输入管理员手机号" />
        </a-form-model-item>
        <a-form-model-item label="管理员姓名" prop="nickname">
          <a-input v-model="form.nickname" placeholder="请输入管理员姓名" />
        </a-form-model-item>
        <a-form-model-item label="集团状态" prop="status">
          <a-radio-group v-model="form.status">
            <a-radio
              :key="item.value"
              :value="item.value"
              v-for="item in enabledArr"
              >{{ item.label }}</a-radio
            >
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
    </my-form-drawer>
  </div>
</template>

<script>
import { rule } from "@/utils/validateRules";
import { add } from "@/services/tenant";
import MyFormDrawer from "@/components/drawer/MyFormDrawer";

export default {
  name: "companyForm",
  components: { MyFormDrawer },
  data() {
    return {
      type: "0", //0添加，1编辑
      title: "",
      form: {
        name: "",
        status: "0",
        phone: "",
        nickname:''
      },
      visible: false,
      rules: {
        name: [
          { required: true, message: "集团名称不能为空", trigger: "change" },
          { validator: rule.validatorNameCn, trigger: "change" }
        ],
        nickname: [
          { required: true, message: "管理员姓名不能为空", trigger: "change" },
          { validator: rule.validatorNameCn, trigger: "change" }
        ],
        phone: [
          { required: true, message: "管理员账号不能为空", trigger: "change" },
          { validator: rule.validatorIsMobile, trigger: "change" }
        ],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
      },
      enabledArr: [
        { value: '0', label: "正常" },
        { value: '9', label: "停用" }
      ],
     
    };
  },
  computed: {},
  created() {
    
  },
  methods: {
     changeForm(state,action,data){
      this.visible = state;
      this.type = action;
      this.title = this.type ? "编辑用户" : "新增用户";
      if (this.type) {
        this.form = data;
      }
    },
    initData() {
      this.form = {
        name: "",
        phone: "",
        status: "0",
        nickname:''
      };
    },
    onClose() {
      this.visible = false;
      this.$refs["form"].clearValidate();
      this.initData();
    },
    edit() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return false;
        }
        add(this.form).then(({data}) => {
          if (data.code === 0) {
            this.$message.success("添加成功");
            this.onClose();
            this.$emit("getList");
          } 
        });
      });
    },
  
  }
};
</script>

<style lang="less" scoped></style>
